package com.gitee.hermer.engine.jars.core.xauth.configuration;

import javax.servlet.FilterRegistration;
import javax.servlet.ServletContext;
import javax.sql.DataSource;

import org.apache.shiro.SecurityUtils;
import org.jasig.cas.client.session.SingleSignOutFilter;
import org.jasig.cas.client.session.SingleSignOutHttpSessionListener;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.jta.JtaTransactionManager;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.context.ServletConfigAware;
import org.springframework.web.context.ServletContextAware;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.gitee.hermer.boot.jee.commons.log.UtilsContext;
import com.gitee.hermer.engine.jars.core.domain.EngineConstant;
import com.gitee.hermer.engine.jars.core.xauth.IXAuthService;
import com.gitee.hermer.engine.jars.core.xauth.config.XAuthProperties;
import com.gitee.hermer.engine.jars.core.xauth.impl.XAuthServiceImpl;
import com.gitee.hermer.engine.jars.core.xauth.interceptor.EngineAuthenticationHandlerInterceptor;
import com.gitee.hermer.engine.jars.dependent.dsm.IEnginePropertiesAware;
import com.gitee.hermer.engine.jars.dependent.redis.config.RedisProperties;
import com.gitee.hermer.engine.jars.dependent.redis.configuration.RedisAutoConfiguration;
import com.jc.support.auth.web.authz.DefaultBizAuthorizationSupporter;
import com.jc.support.auth.web.mgt.DefaultBizWebSecurityManager;
import com.jc.xauth.web.filter.XauthFilterFactoryBean;
import com.jc.xauth.web.interceptor.AuthorizationHandlerInterceptor;

import redis.clients.jedis.JedisPoolConfig;


@Configuration
@Import(value={RedisAutoConfiguration.class})
@ComponentScan(basePackages = {"com.jc.xauth.web.bean"}, useDefaultFilters = false, includeFilters = {@Filter(Controller.class),@Filter(ControllerAdvice.class)})
public class XAuthAutoConfiguration  extends WebMvcConfigurerAdapter implements IEnginePropertiesAware<XAuthProperties>,ApplicationContextAware{

	private XAuthProperties properties;
	private ApplicationContext applicationContext;
	@Autowired
	private EngineAuthenticationHandlerInterceptor authenticationHandlerInterceptor;
	
	@Bean(name=EngineConstant.SPRING_BEAN_NAME_SHIRO_FILTER)
	public XauthFilterFactoryBean getXauthFilterFactoryBean(){
		return new XauthFilterFactoryBean();
	}
	
	@Bean()
	public EngineAuthenticationHandlerInterceptor engineAuthenticationHandlerInterceptor(){
		return new EngineAuthenticationHandlerInterceptor();
	}
	
	
	
	@Bean()
	public IXAuthService authService(){
		return new XAuthServiceImpl();
	}
	
	
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(authenticationHandlerInterceptor).addPathPatterns("/**");
		registry.addInterceptor(new AuthorizationHandlerInterceptor()).addPathPatterns("/**");
	}
	
	
	@Bean(name="securityManager")
	@Autowired
	public DefaultBizWebSecurityManager getBizWebSecurityManager(JedisConnectionFactory factory,DefaultBizAuthorizationSupporter authorizationSupporter){
		DefaultBizWebSecurityManager bean = new DefaultBizWebSecurityManager();
		bean.setSkipAuthenticate(properties.getSkipAuthenticate());
		bean.setSuccessUrl(properties.getSuccessUrl());
		bean.setUnauthorizedUrl(properties.getUnauthorizedUrl());
		bean.setBizSiteUrlPrefix(properties.getBizSiteUrlPrefix());
		bean.setCasServerUrlPrefix(properties.getCasServerUrlPrefix());
		bean.setRedisConnectionFactory(factory);
		bean.setAuthorizationSupporter(authorizationSupporter);
		SecurityUtils.setSecurityManager(bean);
		return bean;
	}

/*	@Bean(name="bizAuthorizationSupporter")
	public DefaultBizAuthorizationSupporter getBizAuthorizationSupporter(){
		DefaultBizAuthorizationSupporter bean = new DefaultBizAuthorizationSupporter();
		return bean;
	}*/


	@Override
	@Autowired
	public void setProperties(XAuthProperties properties) {
		this.properties = properties;
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		this.applicationContext = applicationContext;
	}

}
